<style type="text/css">
	.list-tickets{
		width: 100%;
		vertical-align:central;
	}
	.list-tickets th{
		text-align:left;
	}
	.list-tickets .date{
		width:75px;
		text-align:center;
	}
	.list-tickets .status{
		width:90px;
		text-align:center;
	}
	.table-bordered > thead > tr > th, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > tbody > tr > td, .table-bordered > tfoot > tr > td{
		border:none;
		border-bottom:1px solid #dddddd;
	}
	legend{
		margin-top:20px;
	}
	select{
		margin: 5px 2px;	
	}
	<?php if($arrFilters['filter'] != 'true'): ?>
	.form-group{
		display:none;	
	}
	<?php endif; ?>
	#ui-datepicker-div { font-size: 12px; } 
</style>
<h4>
	<i class="fa fa-tags"></i> 
	<?php echo Yii::t('helpdesk','Support Tickets'); ?> &raquo; <?php if(!empty($dept)): ?><?php echo $dept['category_name']; ?><?php else: ?>All<?php endif; ?>
	<small>[ <?php echo !empty($arrTickets[0]['total_rows'])?$arrTickets[0]['total_rows']:'0';?> ]</small></h4><hr>
<?php 
foreach(Yii::app()->user->getFlashes() as $key => $message){
	echo '<div class="'.$key.'"><p>'.$message."</p></div>\n";
}
?>
<a href="/tickets/create.html<?php if(!empty($dept)): ?>?dept_id=<?php echo $dept['category_id']; ?><?php endif; ?>"><i title="add new" class="glyphicon glyphicon-plus"></i> <?php echo Yii::t('helpdesk','Create ticket'); ?></a>
<form method="get" name="frmFilter" id="frmFilter">
    <fieldset>
        <legend style="cursor:pointer;"><span style="font-size:12px;"><i class="fa <?php if($arrFilters['filter'] != 'true'): ?>fa-plus-square-o<?php else: ?>fa-minus-square-o<?php endif; ?>" id="filterIcon"></i></span> <?php echo Yii::t('helpdesk','Filter'); ?></legend>
        <div class="form-group">
        	<?php if(empty($arrFilters['deptName'])): ?>
            <select onchange="$('#subject').attr('disabled', 'disabled'); $('#frmFilter').submit();" id="dept" name="dept" class="form-inline">
            	<option value="">-- <?php echo Yii::t('helpdesk','Department'); ?> --</option>
				<?php foreach($arrDepts as $R): ?>
                <?php if($R['category_level'] == 0): ?>
                <option value="<?php echo $R['category_id']; ?>" <?php if($arrFilters['dept'] == $R['category_id']): ?>selected<?php endif; ?>><?php echo $R['category_name']; ?></option>
                <?php endif; ?>
				<?php endforeach; ?>
            </select>
            <?php endif; ?>
            
            <?php if($arrFilters['dept'] != ''): ?>
            <select  onchange="$('#frmFilter').submit();" id="subject" name="subject" class="form-inline">
            	<option value="">-- <?php echo Yii::t('helpdesk','Subject'); ?> --</option>
				<?php foreach($arrDepts as $R): ?>
                <?php if($R['category_level'] == 1 && $R['category_parent'] == $arrFilters['dept']): ?>
                <option value="<?php echo $R['category_id']; ?>" <?php if($arrFilters['subject'] == $R['category_id']): ?>selected<?php endif; ?>><?php echo $R['category_name']; ?></option>
                <?php endif; ?>
				<?php endforeach; ?>
            </select>
            <?php endif; ?>
        	
            <select onchange="$('#frmFilter').submit();" name="status" id="status" class="form-inline">
                <option value="">-- <?php echo Yii::t('helpdesk','Status'); ?> --</option>
                <option value="1" <?php if($arrFilters['status'] == '1'): ?>selected<?php endif; ?>><?php echo Yii::t('helpdesk','Open'); ?></option>
                <option value="0" <?php if($arrFilters['status'] == '0'): ?>selected<?php endif; ?>><?php echo Yii::t('helpdesk','Closed'); ?></option>
            </select>
            
            <!--<select onchange="$('#frmFilter').submit();" name="urgency" id="urgency" class="form-inline">
                <option value="">-- Urgency --</option>
                <option value="1" <?php if($arrFilters['urgency'] == '1'): ?>selected<?php endif; ?>>Low</option>
                <option value="2" <?php if($arrFilters['urgency'] == '2'): ?>selected<?php endif; ?>>Medium</option>
            	<option value="3" <?php if($arrFilters['urgency'] == '3'): ?>selected<?php endif; ?>>Hight</option>
            </select>-->
        </div>
        <div class="form-group">
        	<input type="text" name="dfrom" id="dfrom" readonly value="<?php echo $arrFilters['dfrom']; ?>" placeholder="<?php echo Yii::t('helpdesk','Start date'); ?>">
            <input type="text" name="dto" id="dto" readonly value="<?php echo $arrFilters['dto']; ?>" placeholder="<?php echo Yii::t('helpdesk','End date'); ?>">
        </div>
        <div class="form-group">
        	<input type="hidden" name="filter" value="true">
            <input type="text" name="keyword" id="keyword" value="<?php echo $arrFilters['keyword']; ?>" placeholder="<?php echo Yii::t('helpdesk','Keyword'); ?>">
            <input type="submit" value="<?php echo Yii::t('helpdesk','Search'); ?>">
            <input type="button" value="<?php echo Yii::t('helpdesk','Reset'); ?>" onClick="freset();">
        </div>
        <!--
        <div class="form-group">
        	<a> <i class="fa fa-file-excel-o"></i> Xuất Excel</a>
        </div>
        -->
    </fieldset>
</form>
<table class="list-tickets table table-striped table-bordered">
    <tbody>
    	<tr>
            <th class="date"><?php echo Yii::t('helpdesk','Date'); ?></th>
            <th><?php echo Yii::t('helpdesk','Ticket'); ?></th>
            <th class="status"><?php echo Yii::t('helpdesk','Status'); ?></th>
    	</tr>        
        <?php if(!empty($arrTickets)): ?>
        <?php foreach($arrTickets as $R): ?>
        <?php 
			$ticketDepartment = array();
			$ticketSubject = array();
			if(!empty($R['post_category'])){
				$arrPostCateIds = array();
				foreach($R['post_category'] as $C){
					$arrPostCateIds[] = $C['category_id'];
				}
				foreach($arrDepts as $D){
					if(in_array($D['category_id'], $arrPostCateIds)){
						if($D['category_level'] == 0) $ticketDepartment = $D;
						else if($D['category_level'] == 1) $ticketSubject = $D;
					}
				}
			}
		?>
        <tr>
            <td class="date"><?php echo nl2br(date("d/m/y \n H:i:s", strtotime($R['post_created_date']))); ?></td>
            <td>
                <a href="/tickets/<?php echo $R['post_id']; ?>.html"><?php echo ($R['post_status']=='1')?'<b>'.$R['post_title'].'</b>':$R['post_title']; ?></a>
				<br>
				<?php echo !empty($ticketDepartment['category_name'])?$ticketDepartment['category_name']:null ?> <?php echo !empty($ticketSubject['category_name'])?' &raquo; '.$ticketSubject['category_name']:null ?>
                <!--<br>
                <?php echo $R['post_creator_username']; ?>-->
            </td>
            <td class="status">
				<?php echo ($R['post_status']=='1')?'<span class="open">'.Yii::t('helpdesk','Open').'</span>':'<span class="closed">'.Yii::t('helpdesk','Closed').'</span>'; ?>
            	<br>
				<?php 
				if(isset($R['fields']['helpdesk-urgency'])){
					switch($R['fields']['helpdesk-urgency']){
						case '1': echo '<span class="urg_1">'.Yii::t('helpdesk','Low').'</span>'; break;
						case '2': echo '<span class="urg_2">'.Yii::t('helpdesk','Medium').'</span>'; break;
						case '3': echo '<span class="urg_3">'.Yii::t('helpdesk','High').'</span>';
					}
				}
				?>
            </td>
        </tr>
        <?php endforeach; ?>  
        <?php else: ?>
        <tr>
        	<td colspan="5"><?php echo Yii::t('helpdesk','No data found'); ?></td>
        </tr>
        <?php endif; ?>     
	</tbody>
</table>
<?php $this->widget('Pagination', array(
	'pages' => $objPages,
)) ?>
<script type="text/javascript" src="libs/jquery/ui/i18n/datepicker-<?php echo Yii::app()->language; ?>.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$.datepicker.setDefaults( $.datepicker.regional[ "<?php echo Yii::app()->language; ?>" ] );
		$("#dfrom").datepicker({
			dateFormat:'dd/mm/yy',
			maxDate: '0',
			onClose: function( selectedDate ) {
				if(selectedDate != ''){
					$("#dto").datepicker( "option", "minDate", selectedDate );
				}
			}
		});
		$("#dto").datepicker({
			dateFormat:'dd/mm/yy',
			maxDate: '0',
			onClose: function( selectedDate ) {
				if(selectedDate != ''){
					$("#dfrom").datepicker( "option", "maxDate", selectedDate );
				}
			}
		});
		$("#frmFilter legend").click(function(){
			showHide();	
		});	
	});
	function showHide(){
		$("#frmFilter .form-group").toggle("slow");
		if($("#frmFilter #filterIcon").hasClass("fa-plus-square-o")){
			$("#frmFilter #filterIcon").removeClass("fa-plus-square-o").addClass("fa-minus-square-o");	
		} else {
			$("#frmFilter #filterIcon").removeClass("fa-minus-square-o").addClass("fa-plus-square-o"); 
		}
	}
	function freset(){
		$('#frmFilter select').val('');
		$('#frmFilter input[type=text]').val('');
		$('#frmFilter').submit();
	}
</script>